package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class MarketCommodityServiceImpl implements MarketCommodityService {
    @Autowired
    MarketCommodityMapper marketCommodityMapper;
    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketCommodity> findAllCommodity() {
        List<MarketCommodity> marketCommodities = marketCommodityMapper.selectAllCommodity();
        //用户对象赋值
        for (MarketCommodity marketCommodity:marketCommodities) {
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(marketCommodity.getUserId());
            marketCommodity.setUser(centerUser);
        }
        return marketCommodities;
    }

    @Override
    public MarketCommodity finaCommodityByPrimarykey(String commodityid) {
        MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(commodityid);
        //用户赋值
        CenterUser centerUser = centerUserMapper.selectByPrimaryKey(marketCommodity.getUserId());
        marketCommodity.setUser(centerUser);
        return marketCommodity;
    }

    @Override
    public List<MarketCommodity> findAllCommodityByUserId(String userid) {
        List<MarketCommodity> marketCommodities = marketCommodityMapper.selectAllCommodityByUserId(userid);
        return marketCommodities;
    }

    @Override
    public List<MarketCommodity> findCommoditiesBySearch(String search) {
        search="%"+search+"%";
        List<MarketCommodity> marketCommodities = marketCommodityMapper.selectCommoditesBySearch(search);
        //用户对象赋值
        for (MarketCommodity marketCommodity:marketCommodities) {
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(marketCommodity.getUserId());
            marketCommodity.setUser(centerUser);
        }
        return marketCommodities;
    }

    @Override
    public List<MarketCommodity> findIRealease(String loginuserid) {
        List<MarketCommodity> marketCommodities = marketCommodityMapper.selectIReLease(loginuserid);
        return marketCommodities;
    }

    @Override
    public List<MarketCommodity> findISell(String loginuserid) {
        List<MarketCommodity> marketCommodities = marketCommodityMapper.selectISell(loginuserid);
        return marketCommodities;
    }

    @Override
    public int changeCommodity(MarketCommodity commodity) {
        commodity.setStatus(Byte.parseByte("0"));
        commodity.setDate(new Date());
        int i = marketCommodityMapper.updateByPrimaryKey(commodity);
        return i;
    }

    @Override
    public int deleteCommodityById(String commodityid) {
        int i = marketCommodityMapper.deleteById(commodityid);
        return i;
    }

    @Override
    public MarketCommodity postCommodity(MarketCommodity marketCommodity) {
        marketCommodity.setId(KeyUtil.generateKey());
        marketCommodity.setDate(new Date());
        marketCommodity.setHeatStatus(Byte.parseByte("0"));
        int insert = marketCommodityMapper.insert(marketCommodity);
        MarketCommodity commodity = marketCommodityMapper.selectByPrimaryKey(marketCommodity.getId());
        return commodity;
    }

    @Override
    public int polishCommodity(String commodityid) {
        int i = marketCommodityMapper.updateHeatStatusByPrimaryKey(commodityid);
        return i;
    }

    @Override
    public int changeCommodityHeatStatus() {
        int i = marketCommodityMapper.updateCommodityHeatStatus();
        return i;
    }


}
